package org.zsqx.tools;

import java.lang.reflect.Field;

import org.zsqx.bean.Disaster;
import org.zsqx.bean.DisasterUpload;

import cDao.CdaoFactory;
import cDao.HelpDao;
import cDao.RealDao;

public class DisTools {
	
	public DisasterUpload changeDis(Integer[] disIds){
		RealDao rd = CdaoFactory.getRealDao();
		DisasterUpload du = new DisasterUpload();
		for (Integer id : disIds) {
			Disaster dis = (Disaster) rd.findById(new Disaster(id));
			sumClass(du, dis);
		}
		return du;
	}
	public void sumClass(DisasterUpload du,Disaster dis){
		Class duClass = du.getClass();
		Class disClass = dis.getClass();
		Field[] fields = duClass.getDeclaredFields();
		for (Field field : fields) {
			Class fieldType = field.getType();
			String duGetMethodName = HelpDao.getGetMethod(field.getName());
			String duSetmethodName = HelpDao.getSetMethod(field.getName());
			String disGetMethodName = HelpDao.getGetMethod(field.getName());
			Object duValue = HelpDao.methodInvoke(duClass, du, duGetMethodName);
			Object disValue = HelpDao.methodInvoke(disClass, dis, disGetMethodName);
			if(duValue==null||disValue==null){
				duValue = disValue;
			}else{
				if(fieldType == String.class){
					duValue = duValue.toString() + disValue.toString();
				}
				if(fieldType == Double.class){
					duValue = (Double)duValue +(Double)disValue;
				}
			}
			HelpDao.methodInvoke(duClass, du, duSetmethodName, fieldType, duValue);
		}
	}
	
	public static void main(String[] args) {
		DisTools dt = new DisTools();
		Integer[] ids = {36,37,38,39};
		DisasterUpload du  =  dt.changeDis(ids);
	}

}
